<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<c:set var="ctx" value="${pageContext.request.contextPath }"/>
<div class="page-header">
    <button id="btnAdd" type="button" class="btn btn-primary btn-sm">
        <i class="fa fa-user-plus"></i>&nbsp;添加
    </button>
    <button id="btnEdit" type="button" class="btn btn-success btn-sm">
        <i class="fa fa-pencil-square-o"></i>&nbsp;编辑
    </button>
    <button id="btnDel" type="button" class="btn btn-danger btn-sm">
        <i class="fa fa-trash-o"></i>&nbsp;删除
    </button>
    <button id="btnSetResource" type="button" class="btn btn-info btn-sm">
        <i class="fa fa-gears"></i>&nbsp;分配权限
    </button>
    <%--<button id="btnRefresh" type="button" class="btn btn-grey btn-sm">
        <i class="fa fa-refresh"></i>&nbsp;刷新权限
    </button>--%>
</div>
<%--<div class="input-group">
     <input id="searchKey" type="text" class="input form-control" placeholder="角色名称...">
     <span class="input-group-btn">
         <button id="btnSearch" class="btn btn-primary btn-sm" type="button"> <i class="fa fa-search"></i> 搜索</button>
     </span>
</div>--%>
<div class="row" style="margin-top:5px;">
    <div class="col-xs-12 widget-container-col ui-sortable"
         style="min-height: 127px;">
        <!-- #section:custom/widget-box.options.transparent -->
        <div class="widget-box transparent ui-sortable-handle"
             style="opacity: 1; z-index: 0;">
            <div class="widget-header">
                <h4 class="widget-title lighter">角色列表</h4>
                <div class="widget-toolbar no-border">
                    <a href="#" data-action="fullscreen" class="orange2">
                        <i class="ace-icon fa fa-arrows-alt"></i>
                    </a>
                    <a href="#" data-action="collapse" class="green">
                        <i class="ace-icon fa fa-chevron-up"></i>
                    </a>
                </div>
            </div>

            <div class="widget-body" style="display: block;">
                <div class="widget-main padding-6 no-padding-left no-padding-right">
                    <input id="pageNum" type="hidden" value="${page.pageNum }">
                    <input id="pageSize" type="hidden" value="${page.pageSize }">
                    <input id="orderByColumn" type="hidden" value="${page.orderByColumn }">
                    <input id="orderByType" type="hidden" value="${page.orderByType }">
                    <div id="dtGridContainer" class="dlshouwen-grid-container"></div>
                    <div id="dtGridToolBarContainer" class="dlshouwen-grid-toolbar-container"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var dtGridColumns = [{
        id: 'id',
        title: '序号',
        type: 'number',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        fastQuery: true,
        fastQueryType: 'lk',
        /*resolution: function (value, record, column, grid, dataNo, columnNo) {
            return (grid.pager.nowPage - 1) * grid.pager.pageSize + dataNo + 1;
        }*/
    }, {
        id: 'name',
        title: '角色名称',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        fastQuery: true,
        fastQueryType: 'lk'
    }, {
        id: 'key',
        title: '角色标识',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header'
    }, {
        id: 'status',
        title: '角色状态',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'xs',
        codeTable: {0: "正常", 1: "已删除"},
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value === 0) {
                return '<span class="label label-sm label-success arrowed arrowed-righ">正常</span>';
            } else {
                return '<span class="label label-sm label-warning arrowed arrowed-righ">已删除</span>';
            }
        }
    }, {
        id: 'description',
        title: '角色描述',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'xs',
        fastQuery: true,
        fastQueryType: 'lk'
    }, {
        id: 'createTime',
        title: '创建时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        otype: 'string',
        oformat: 'yyyy-MM-dd hh:mm:ss',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs',
        fastQuery: true,
        fastQueryType: 'lk'
    }, {
        id: 'updateTime',
        title: '更新时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        otype: 'string',
        oformat: 'yyyy-MM-dd hh:mm:ss',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs|md',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value === null) {
                return '';
            } else {
                return value;
            }
        }
    }];

    //动态设置jqGrid的rowNum
    var pageSize = $("#pageSize").val();
    pageSize = pageSize == 0 || pageSize == "" ? sys.pageNum : pageSize;

    var dtGridOption = {
        lang: 'zh-cn',
        ajaxLoad: true,
        check: true,
        checkWidth: '37px',
        extraWidth: '37px',
        loadURL: sys.rootPath + '/role/list.html',
        columns: dtGridColumns,
        gridContainer: 'dtGridContainer',
        toolbarContainer: 'dtGridToolBarContainer',
        tools: 'fastQuery|refresh|print|export[excel]',
        exportFileName: '角色信息',
        pageSize: pageSize,
        pageSizeLimit: [10, 20, 30]
    };

    var grid = $.fn.dlshouwen.grid.init(dtGridOption);

    $(function () {
        if (null != $("#orderByColumn").val() && '' != $("#orderByColumn").val()) {
            grid.sortParameter.columnId = $("#orderByColumn").val();
            grid.sortParameter.sortType = $("#orderByType").val();
        }

        grid.load();

        initButtonClick();
    });


    /**
     * 初始化绑定按钮事件
     */
    function initButtonClick() {
        $("#btnAdd").click(function () {
            movic.common.addModel('/role/addUI.html');
        });

        $("#btnEdit").click(function () {
            movic.common.editModel('/role/editUI.html');
        });

        $("#btnDel").click(function () {
            delModel('/role/deleteBatch.html', customSearch);
        });

        $("#btnSetResource").click(function () {
            movic.common.editModel('/role/permissionUI.html');
        });

        //绑定刷新权限事件
        $("#btnRefresh").click(function () {
            refresh();
        });

        $("#btnSearch").click(customSearch);

        //注册回车键事件
        /*document.onkeypress = function(e) {
            var ev = document.all ? window.event : e;
            if(ev.keyCode == 13) {
                customSearch();
            }
        };*/
    }

    /**
     * 权限刷新
     **/
    function refresh() {
        var index;
        $.ajax({
            type: "GET",
            url: sys.rootPath + "/role/refresh.html",
            dataType: "json",
            beforeSend: function () {
                index = layer.load();
            },
            success: function (result) {
                layer.close(index);
                if (result.code === 200) {
                    layer.msg(result.message, {
                        icon: 1
                    });
                } else {
                    layer.msg(result.message, {
                        icon: 5
                    });
                }
            },
            error: function (errorMsg) {
                layer.close(index);
                layer.msg('服务器未响应,请稍后再试', {
                    icon: 3
                });
            }
        });
    }

    /**
     * 自定义查询
     * 这里不传入分页信息，防止删除记录后重新计算的页码比当前页码小而导致计算异常
     */
    function customSearch() {
        grid.parameters = new Object();
        grid.parameters['name'] = $("#searchKey").val();
        grid.refresh(true);
    }

    /**
     * 删除
     */
    function delModel(nav, callback) {
        var rows = grid.getCheckedRecords();

        if (rows.length >= 1) {
            layer.confirm('确认删除吗？', {
                icon: 3,
                title: '删除提示'
            }, function (index, layero) {
                var delete_ids = [];
                for (var i = 0; i < rows.length; i++) {
                    delete_ids.push(rows[i].id);
                }
                var loadUI;
                $.ajax({
                    type: "POST",
                    url: sys.rootPath + nav,
                    data: {
                        "ids": delete_ids.join(',')
                    },
                    dataType: "json",
                    beforeSend: function () {
                        loadUI = layer.load();
                    },
                    success: function (result) {
                        layer.close(loadUI);
                        if (result.code === 200) {
                            layer.msg(result.message, {
                                icon: 1
                            });
                            if (callback) {
                                callback();
                            }
                        } else {
                            layer.msg(result.message, {
                                icon: 5
                            });
                        }
                    },
                    error: function (errorMsg) {
                        layer.close(loadUI);
                        layer.msg('服务器未响应,请稍后再试', {
                            icon: 3
                        });
                    }
                });
                layer.close(index);
            })

        } else {
            layer.msg("你没有选择数据", {
                icon: 0
            });
        }
    }

</script>